<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>修改轮播图</title>
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
    <!-- import Vue before Element -->
    <script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
    <!-- 引入axios -->
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
    <!-- import JavaScript -->
    <script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
</head>
<body>
<div id="app">
    <el-page-header
            style="background-color: #0096dc;color:white;line-height: 60px"
            @back="goBack" content="修改轮播图">
    </el-page-header>
    <!--显示原来的图片-->
    <img :src="oldUrl" alt="">
    <el-upload
            action="/upload"
            name="picFile"
            limit="1"
            list-type="picture-card"
            :on-success="handleSuccess"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove">
        <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
    <el-button type="success" @click="update()">修改轮播图</el-button>
</div>
</body>
<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                banner: {
                    url: ""
                },
                oldUrl:""
            }
        },
        methods: {
            goBack() {
                // 返回上一页面
                history.back()
            },

            update() {
                if (v.banner.url == "") {
                    v.banner.$message.error("请选择上传的图片");
                    return;
                }
                // 得到地址栏中的id
                let id = location.search.split("=")[1];
                // 把id转进轮播图对象中
                v.banner.id = id;
                // 发出请求
                axios.post("/banner/update",v.banner).then(function (){
                    v.$message.success("添加成功")
                    // 添加完成返回后台管理页面
                    location.href="/admin.html"
                })
            },
            handleSuccess(response, file, fileList) {
                // response代表图名
                v.banner.url = response;
            }
        },
        created:function (){
            // 发请求获取需要修改的轮播图url
            axios.get("/banner/selectById"+location.search).then(function (response){
                v.oldUrl = response.data;
            })
        }
    })
</script>
</html>